package com.lxx.wiki.service;

import com.lxx.wiki.domain.entity.Ebook;
import com.lxx.wiki.domain.entity.LoginUser;
import com.lxx.wiki.domain.entity.User;
import com.lxx.wiki.domain.entity.UserExample;
import com.lxx.wiki.domain.req.UserLoginReq;
import com.lxx.wiki.domain.req.UserQueryReq;
import com.lxx.wiki.domain.req.UserResetPasswordReq;
import com.lxx.wiki.domain.req.UserSaveReq;
import com.lxx.wiki.domain.resp.PageResp;
import com.lxx.wiki.domain.resp.UserQueryResp;
import com.lxx.wiki.exception.BusinessException;
import com.lxx.wiki.exception.BusinessExceptionCode;
import com.lxx.wiki.mapper.UserMapper;
import com.lxx.wiki.utils.BeanCopyUtils;
import com.lxx.wiki.utils.JwtUtil;
import com.lxx.wiki.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;



    public List<UserQueryResp> list(UserQueryReq req) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();

        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andNameLike("%" + req.getLoginName() + "%");
        }

        List<User> userList = userMapper.selectByExample(userExample);
        List<UserQueryResp> userQueryRespList = BeanCopyUtils.copyBeanList(userList, UserQueryResp.class);

        return userQueryRespList;


    }

    public void save(UserSaveReq req) {
        User user = BeanCopyUtils.copyBean(req, User.class);
        if (ObjectUtils.isEmpty(user.getId())) {
            // 新增
            User userDB = selectByLoginName(req.getLoginName());
            if (ObjectUtils.isEmpty(userDB)) {
                // id数据库设置了自增
                userMapper.insert(user);
            } else {
                // 用户名已存在,需要告诉用户
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }

        } else {
            // 更新 (需求，在更新时不允许修改用户名，不允许修改密码只能通过重置功能)
            user.setLoginName(null); // 强制去掉名称
            user.setPassword(null);// 强制去掉密码
            userMapper.updateByPrimaryKeySelective(user);
        }
    }

    public User selectByLoginName(String LoginName) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }

    public void delete(Long id) {
        userMapper.deleteByPrimaryKey(id);
    }



}
